package com.example.smartcity.ui.smartCity.activitytModule

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity.R
import com.example.smartcity.databinding.ActivityCameraBinding
import com.example.smartcity.databinding.ActivitySspBinding
import com.example.smartcity.logic.dao.SspDataSqlite
import com.example.smartcity.ui.smartCity.Adapter.SmRecyclerViewAdapter
import com.example.smartcity.ui.smartCity.model.Sm

class SspActivity : AppCompatActivity() {
    val dbHelper = SspDataSqlite(this, "ssp.db", 5)

    private lateinit var mBinding: ActivitySspBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = ActivitySspBinding.inflate(layoutInflater)
        setContentView(mBinding.root)
        title = "随手拍"
        supportActionBar?.setDisplayHomeAsUpEnabled(true)

        // 点击浮动按钮打开相机
        mBinding.SspFab.setOnClickListener {
            startActivity(Intent(this, CameraActivity::class.java))
        }
    }


    // 加载数据
    @SuppressLint("Range")
    override fun onStart() {
        super.onStart()
        val re = mBinding.SspRecyclerView

        val db = dbHelper.writableDatabase

        // 查询数据库中所有的数据
        val ssp = db.query("ssp", null, null, null, null, null, null)

        val array = ArrayList<Sm>()

        if (ssp.moveToFirst()){
            do {
                val title = ssp.getString(ssp.getColumnIndex("title"))
                val contend = ssp.getString(ssp.getColumnIndex("content"))
                array.add(Sm(title, contend, "2023.1.1"))
            }while (ssp.moveToNext())
        }

        val layout = LinearLayoutManager(this)
        re.layoutManager = layout
        val adapter = SmRecyclerViewAdapter(array)
        re.adapter = adapter
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        this@SspActivity.finish()
        return super.onOptionsItemSelected(item)
    }
}